package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GcqfqdklqddhsclzdlzysljlbBsDetailBo;
import com.ruoyi.report_gc.domain.vo.GcqfqdklqddhsclzdlzysljlbBsDetailVo;
import com.ruoyi.report_gc.domain.GcqfqdklqddhsclzdlzysljlbBsDetail;
import com.ruoyi.report_gc.mapper.GcqfqdklqddhsclzdlzysljlbBsDetailMapper;
import com.ruoyi.report_gc.service.IGcqfqdklqddhsclzdlzysljlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情Service业务层处理
 *
 * @author llb
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class GcqfqdklqddhsclzdlzysljlbBsDetailServiceImpl implements IGcqfqdklqddhsclzdlzysljlbBsDetailService {

    private final GcqfqdklqddhsclzdlzysljlbBsDetailMapper baseMapper;

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情
     */
    @Override
    public GcqfqdklqddhsclzdlzysljlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情列表
     */
    @Override
    public TableDataInfo<GcqfqdklqddhsclzdlzysljlbBsDetailVo> queryPageList(GcqfqdklqddhsclzdlzysljlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<GcqfqdklqddhsclzdlzysljlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情列表
     */
    @Override
    public List<GcqfqdklqddhsclzdlzysljlbBsDetailVo> queryList(GcqfqdklqddhsclzdlzysljlbBsDetailBo bo) {
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsDetail> buildQueryWrapper(GcqfqdklqddhsclzdlzysljlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GcqfqdklqddhsclzdlzysljlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getGongczj() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getGongczj, bo.getGongczj());
        lqw.eq(bo.getGongcjmj() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getGongcjmj, bo.getGongcjmj());
        lqw.eq(bo.getQvfhz() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getQvfhz, bo.getQvfhz());
        lqw.eq(bo.getQvfqd() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getQvfqd, bo.getQvfqd());
        lqw.eq(bo.getJixhz() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getJixhz, bo.getJixhz());
        lqw.eq(bo.getKanglqd() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getKanglqd, bo.getKanglqd());
        lqw.eq(bo.getYuanszb() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getYuanszb, bo.getYuanszb());
        lqw.eq(bo.getDuanhbj() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getDuanhbj, bo.getDuanhbj());
        lqw.eq(bo.getDuanhscl() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getDuanhscl, bo.getDuanhscl());
        lqw.eq(bo.getZuidlbj() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getZuidlbj, bo.getZuidlbj());
        lqw.eq(bo.getZuidldhbj() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getZuidldhbj, bo.getZuidldhbj());
        lqw.eq(bo.getZuidlzysl() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getZuidlzysl, bo.getZuidlzysl());
        lqw.eq(bo.getQiangqb() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getQiangqb, bo.getQiangqb());
        lqw.eq(bo.getQvqb() != null, GcqfqdklqddhsclzdlzysljlbBsDetail::getQvqb, bo.getQvqb());
        lqw.eq(StringUtils.isNotBlank(bo.getDaunltz()), GcqfqdklqddhsclzdlzysljlbBsDetail::getDaunltz, bo.getDaunltz());
        return lqw;
    }

    /**
     * 新增钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情
     */
    @Override
    public Boolean insertByBo(GcqfqdklqddhsclzdlzysljlbBsDetailBo bo) {
        GcqfqdklqddhsclzdlzysljlbBsDetail add = BeanUtil.toBean(bo, GcqfqdklqddhsclzdlzysljlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情
     */
    @Override
    public Boolean updateByBo(GcqfqdklqddhsclzdlzysljlbBsDetailBo bo) {
        GcqfqdklqddhsclzdlzysljlbBsDetail update = BeanUtil.toBean(bo, GcqfqdklqddhsclzdlzysljlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GcqfqdklqddhsclzdlzysljlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢材屈服强度、抗拉强度、断后伸长率、最大力总延伸率试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
